/*
 * MAJOR TODO: This file shouldn't contain as much code as it does now
 * move whatever possible to a better place'
 */
#include <allegro.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "menu/MainMenu.h"
#include "common/helpers.h"
#include "game/managers/DrawManager.h"
#include "game/entities/Rock.h"
#include "common/GameScreen.h"
#include "input/KeyboardHandler.cpp"
 
using namespace std;

ScreenManager *screenMgr;
int activeScreen = 0; 
IScreen *mainMenu;
IScreen *gameScreen;
KeyboardHandler * keyboardHandler;

void helloworld(){
    cout << "Hello world\n";
}

void initialize() {
    CommonResource::getInstance()->setKeyboardHandler(new KeyboardHandler());
    mainMenu = new MainMenu();
    gameScreen = new GameScreen();
    screenMgr = CommonResource::getInstance()->getScreenManager();
    activeScreen = CommonResource::getInstance()->getActiveScreen(); 
    keyboardHandler = CommonResource::getInstance()->getKeyboardHandler(); 

    screenMgr->addScreen(0, mainMenu);
    screenMgr->addScreen(1, gameScreen);
    keyboardHandler->registerKey(KEY_SPACE, helloworld);
}



static void handleMouse(int flags) {
    if(flags == MOUSE_FLAG_LEFT_DOWN) {
        if(screenMgr->getActiveScreen() == mainMenu) {
            MainMenu* tempmenu = (MainMenu*) screenMgr->getActiveScreen();
            list<MenuItem*> menuItems = tempmenu->getMenuItems();            
            list<MenuItem*>::iterator it = menuItems.begin();
            for(; it != menuItems.end(); it++) {
                if((*it)->posIsOnItem(mouse_x, mouse_y)) {
                    (*it)->execute();
                }
            }
        }
    }
}

static int handleKeyboard(int flags){
    keyboardHandler->handle(flags);
    return 1;
}

int main(void) {
    if (allegro_init() != 0)
        return 1;

    /* install keyboard and mouse */
    install_keyboard();
    install_mouse();


    /* Init graphics and such */
    //set_color_conversion(/*COLORCONV_KEEP_TRANS*/);
    set_color_depth(24);
    if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0) {
        if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
            set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
            allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
            return 1;
        }
    }

    initialize();
    /* clear the screen to white */
    clear_to_color(screen, makecol(255, 255, 255));      

    /* sets up the keyhandler and registers handleKeyboard() as the callback-routine */    
    keyboard_callback = handleKeyboard; 

    /* Mouse-related things */
    enable_hardware_cursor();
    select_mouse_cursor(MOUSE_CURSOR_ARROW);
    show_mouse(screen); //shows the mouse on 'screen'
    mouse_callback = handleMouse; //registers the callback-routine

    blit(screenMgr->getActiveScreen()->draw(),screen, 0,0,0,0,800,600);
    
    while(CommonResource::getInstance()->getQuit() != true) {        
        blit(screenMgr->getActiveScreen()->draw(),screen, 0,0,0,0,800,600);
        rest(16); // 1000/ 16 ~= 60fps
    }

    return 0;
}

END_OF_MAIN();

